package com.bjsxt.sm.mvc.carController;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.sm.common.controller.BaseController;
import com.bjsxt.sm.common.util.ConstatFinalUtil;
import com.bjsxt.sm.common.util.PageInfoUtil;
import com.bjsxt.sm.users.pojo.Acars;
import com.bjsxt.sm.users.service.IUsersService;


@Controller
@RequestMapping("/carmanage")
public class CarsController extends BaseController
{	@Resource
	private IUsersService usersService;
	@RequestMapping("/addCarInfo")
	public String addCarInfo()
	{
		
		return "/carmanage/addCarInfo";
		
	}
	@RequestMapping("searcherCarInfo")
	public String searcherCarInfo()
	{
		return "/carmanage/searcherCarInfo";
	}
	/*添加汽车信息*/
	@RequestMapping("/addCarInfoSubmit")
	public String addCarInfoSubmit(Acars acars,MultipartFile file,Model model,HttpServletRequest request) throws IllegalStateException, IOException
	{
		ConstatFinalUtil.LOGGER.info("======addCarInfo========"); 
		file.getOriginalFilename();
		file.transferTo(new File("f:/java高级学习/05 项目(5天)/01 项目 第一天/04 代码/car-rental/WebContent/carimage/7.jpg"));
		/*为jsp上面没有赋值的属性在后台赋值*/
			acars.setUpdateTime(new Date());
			acars.setCreateTime(new Date());
			acars.setImgPath("carimage/7.jpg");
			JSONObject resultJSON=this.usersService.saveOneAcarsService(acars);
			String info=resultJSON.get("info") + "";
			/*将作用域存储到request*/
			model.addAttribute("info",info); 
			/*为了查询角色的相关信息*/
		return "/carmanage/addCarInfo";
	}
	/*查询汽车信息*/
	@RequestMapping("/searcherCarInfoSubmit")
	public String searcherCarInfoSubmit(String keyword,String stats,String co,String pr
			,String currentPage , String pageSize,Model model)
	{
		ConstatFinalUtil.LOGGER.info("====searcherCarInfoSubmit====");
		/*如果不穿默认可能是null*/
		if(keyword==null)
		{
			keyword="";
		} 
		if(stats==null)
		{
			stats="";
		}
		if(co==null)
		{
			co="";
		}
		if(pr==null)
		{
			pr="";
		}
		/* 条件 应该问JSP要,从Request中获取*/
		Map<String, Object> condMap = new HashMap<String,Object>();
		/* 按照车号查询
		 * 模糊查询的问题 
		 * */
		if(!"".equalsIgnoreCase(keyword))
		{
			condMap.put("keyword", keyword);
		}
		/* 按照状态查询 */
		if(!"".equalsIgnoreCase(stats))
		{
			condMap.put("stats", stats);
		}
		/* 按照颜色查询 */
		if(!"".equalsIgnoreCase(co))
		{
			condMap.put("co", co);
		}
		/* 按照价格查询 */
		if(!"".equalsIgnoreCase(pr))
		{
			condMap.put("pr", pr);
		}
		PageInfoUtil pageInfoUtil= proccedPageInfoUtil(currentPage, pageSize);
	List<Acars> acarsList=this.usersService.findCondListAcarsService(pageInfoUtil, condMap);
				/* 查询出的数据存储到四大作用域名中 */
				model.addAttribute("acarsList",acarsList);
				model.addAttribute("pageInfoUtil", pageInfoUtil);
				/* 存储所有的条件 */
				model.addAttribute("keyword", keyword);
				model.addAttribute("stats", stats);
				model.addAttribute("co", co);
				model.addAttribute("pr", pr);
		return "/carmanage/searcherCarInfo";
	}
}
